package com.bitsemart;

import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Marcos on 22/04/2015.
 */
public class VideojuegoAdapter extends RecyclerView.Adapter<VideojuegoAdapter.ViewHolder>{
    int posicion_borrar;

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
        public TextView title;
        public TextView price;
        public CircleImageView image;
        private View view;

        public ViewHolder(View v) {
            super(v);
            view = v;
            title = (TextView) v.findViewById(R.id.title);
            price = (TextView) v.findViewById(R.id.price);
            image = (CircleImageView) v.findViewById(R.id.image);
            v.setOnCreateContextMenuListener(this);
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                                        ContextMenu.ContextMenuInfo menuInfo) {
            menu.add(0, 0, 0, "Borrar juego");
        }

        public void setContextMenuOn(boolean value) {
            if (value) view.setOnCreateContextMenuListener(this);
            else view.setOnCreateContextMenuListener(null);
        }
    }

    private ArrayList<Videojuego> dataset;

    public VideojuegoAdapter (ArrayList<Videojuego> dataset) {
        this.dataset = dataset;
    }

    @Override
    public VideojuegoAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
        View v = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.videojuego_adapter_item, parent, false);
        return new ViewHolder(v);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        // - get element from your dataset at this position
        final Videojuego game = dataset.get(position);
        // - replace the contents of the view with that element
        holder.title.setText(game.getTitulo());
        if (game.getPrecio() == 0) holder.price.setText("Gratis");
        else holder.price.setText(String.format("Precio: %.2f euros", game.getPrecio() * 0.01));
        Bitmap imagen = game.getImagen();
        holder.image.setImageBitmap(imagen);
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                holder.setContextMenuOn(MainActivity.getAdminMode());
                setPositionBorrar(position);
                return false;
            }
        });
    }

    public int getPositionBorrar() {
        return posicion_borrar;
    }

    public void setPositionBorrar(int posicion_borrar) {
        this.posicion_borrar = posicion_borrar;
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return dataset.size();
    }
}
